/*
	This file is part of SRServerLibrary.
	Foobar is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	SRServerLibrary is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with SRServerLibrary.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "SRMessenger.h"
#include "SRPluginLoader.h"
#include <QDebug>

using namespace std;


SRMessenger::SRMessenger(SRPluginLoader *pl, SRSettings *s)
{
	pluginLoader = pl;
	settings = s;
}


SRCommandList SRMessenger::internalBroadcast(SRCommand c)
{
	SRCommandList cl = SRCommandList();

	QList<SRPluginInterface*> plugins = pluginLoader->getPlugins();

	// forward to all plugins
	for(int i = 0; i < plugins.size(); i++)
	{
		qDebug() << "about to send command";
		SRCommandList responseCommands; // the plugin's response
		responseCommands = plugins.at(i)->doCommand(c);
		cl.append(responseCommands);
		qDebug() << "command sent";
		qDebug() << "response from this plugin: " << responseCommands.toString();
	}

	return cl;
}


SRCommandList SRMessenger::internalBroadcast(SRCommandList cl)
{
	SRCommandList joinedCommandList = SRCommandList();
	for(int i=0; i < cl.size(); i++)
	{
		SRCommandList tempList = internalBroadcast(cl.at(i));
		joinedCommandList.append(tempList);
	}

	return joinedCommandList;
}


SRCommandList SRMessenger::sendTo(SRPluginInterface* plugin, SRCommand c)
{
	return plugin->doCommand(c);
}


SRCommandList SRMessenger::sendTo(QString pluginName, SRCommand c)
{
	return SRCommandList();
}

